using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.WizardFramework;
using System.ComponentModel.Design;
using System.Collections;
using Microsoft.Practices.RepositoryFactory.CodeGeneration.ObjectModel;
using Microsoft.Practices.RepositoryFactory.CodeGeneration.ObjectModel.Mappings.Helper;
using Resources=Microsoft.Practices.RepositoryFactory.Properties.Resources;

namespace Microsoft.Practices.RepositoryFactory.CustomPages
{
    public partial class EntitiesFromClassesSelectorCP : CustomWizardPage
    {
        #region Fields

        private ArrayList entities;

        #endregion

        #region Arguments

        /// <summary>
        /// Gets or sets the entities.
        /// </summary>
        /// <value>The entities.</value>
        [RecipeArgument]
        public ArrayList Entities
        {
            get
            {
                return this.entities;
            }
            set
            {
                this.entities = value;
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="T:EntitiesFromClassesSelectorCP"/> class.
        /// </summary>
        /// <param name="parent">The parent.</param>
        public EntitiesFromClassesSelectorCP(WizardForm parent)
            : base(parent)
        {
            InitializeComponent();
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Called when [activated].
        /// </summary>
        public override void OnActivated()
        {
            FillEntitiesListBox();
            SaveArgument();
            base.OnActivated();
        }

        /// <summary>
        /// 	<see cref="P:Microsoft.WizardFramework.WizardPage.IsDataValid"/>
        /// Search for all the arguments that this Wizard gather
        /// If all of them are not null, then the Wizard can finish
        /// </summary>
        /// <value></value>
        public override bool IsDataValid
        {
            get
            {
                return (lstEntities.CheckedItems.Count > 0);
            }
        }

        private void lstEntities_MouseLeave(object sender, EventArgs e)
        {
            SaveArgument();
        }
        #endregion

        #region Private Implementation

        private void FillEntitiesListBox()
        {
            IDictionaryService dictservice = (IDictionaryService)GetService(typeof(IDictionaryService));
            ArrayList entities = (ArrayList)dictservice.GetValue("ProjectEntities");
            ArrayList clonedEntities = ClonerHelper.CloneEntities(entities);

            lstEntities.DisplayMember = Resources.EntityNameDisplayMember;
            lstEntities.Items.Clear();

            foreach(Entity entity in clonedEntities)
            {
                lstEntities.Items.Add(entity);
            }

            SelectItems();
        }

        private void SelectItems()
        {
            IDictionaryService dictservice = (IDictionaryService)GetService(typeof(IDictionaryService));
            ArrayList entities = (ArrayList)dictservice.GetValue("Entities");

            if(entities != null)
            {
                for(int index = 0; index < lstEntities.Items.Count; index++)
                {
                    if(FindEntity(entities, ((Entity)lstEntities.Items[index]).Name) != null)
                    {
                        lstEntities.SetItemChecked(index, true);
                    }
                }
            }
            else
            {
                if(lstEntities.Items.Count > 0)
                {
                    lstEntities.SelectedIndex = 0;
                }
            }
        }

        private ArrayList GetEntities()
        {
            ArrayList entities = new ArrayList();

            foreach(object item in lstEntities.CheckedItems)
            {
                entities.Add((Entity)item);
            }

            return entities;
        }

        private void SaveArgument()
        {
            ArrayList entities = GetEntities();
            IDictionaryService dictservice = (IDictionaryService)GetService(typeof(IDictionaryService));
            dictservice.SetValue("Entities", entities);
        }
        #endregion

        #region Private Finders

        private Entity FindEntity(ArrayList entities, string entityName)
        {
            foreach(Entity entity in entities)
            {
                if(entity.Name.Equals(entityName, StringComparison.InvariantCultureIgnoreCase))
                {
                    return entity;
                }
            }

            return null;
        }
        #endregion
    }
}
